/**
 * Title:        代理数据同步
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.general.Util;



public class CurOptIslasttimeCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("HtmlInfoCheck  start");

        List resultList = new ArrayList();
        String result = "";
        //没有islasttimes为1的数据
        String sqlStr = "";
        sqlStr +=" select workflow_requestbase.requestname, a.requestid  ";
        sqlStr +="   from workflow_currentoperator a  ";
        sqlStr +="  inner join workflow_requestbase  ";
        sqlStr +="     on workflow_requestbase.requestid = a.requestid  ";
        sqlStr +=" inner join hrmresource  ";
        sqlStr +=" on hrmresource.id = a.userid  ";
        sqlStr +="  where (islasttimes = 0 and not exists  ";
        sqlStr +="         (select 1  ";
        sqlStr +="            from workflow_currentoperator b  ";
        sqlStr +="           where a.requestid = b.requestid  ";
        sqlStr +="             and a.userid = b.userid  ";
        sqlStr +="             and b.islasttimes = 1)) and a.usertype=0   ";
        sqlStr +=" union   ";
        sqlStr +=" select workflow_requestbase.requestname, a.requestid  ";
        sqlStr +="   from (select requestid,userid from  workflow_currentoperator where islasttimes = 1 and usertype=0  group by requestid,userid having count(1) > 1) a  ";
        sqlStr +="  inner join workflow_requestbase  ";
        sqlStr +="     on workflow_requestbase.requestid = a.requestid  ";
        sqlStr +=" inner join hrmresource  ";
        sqlStr +=" on hrmresource.id = a.userid  ";
        recordSet.executeSql(sqlStr);
        
        String requestidtemp = "";
        String useridtemp = "";
        String indexdescshow = "";
        //其他标签内容拼接
        String indexdescCact = "";
        int cnt = 0;
        while(recordSet.next()){
            String id = recordSet.getString("id");
            String requestid = recordSet.getString("requestid");
            if(!requestid.equals(requestidtemp)){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage", "请求[" + recordSet.getString("requestname") + "]中islasttime存在问题，需要修复。");
                resultMap.put("requestid",requestid);
                resultMap.put("buttonname","修复");
                resultList.add(resultMap);
                requestidtemp = requestid;
            }
            cnt++;
        }
        if(cnt == 0){
            result = resultOk;
        }
        (new BaseBean()).writeLog("HtmlInfoCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        
        if(dataMap!= null && dataMap.get("divid")!= null){
            /*
            //根据参数获取列表
            String curoptid = (String)dataMap.get("curoptid");
            String indexdesc =  (String)dataMap.get("indexdesc");
            String divid =  (String)dataMap.get("divid");
            
            List resultList = new ArrayList();
            String result = "";

            recordSet.executeUpdate("update workflow_currentoperator set islasttimes=1  where id = " + curoptid );

            String sqlStr = "";
            sqlStr +=" select islasttimes from workflow_currentoperator where id = " + curoptid ;
            recordSet.executeSql(sqlStr);
            
            if(recordSet.next() && recordSet.getInt("islasttimes") == 1){
                result = resultOk;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("result",resultOk);
                resultMap.put("fixMessage","数据修复成功");
                resultMap.put("divid",divid);
                resultList.add(resultMap);
            }else{
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("result",resultNeedFix);
                resultMap.put("fixMessage","数据修复失败");
                resultMap.put("divid",divid);
                resultList.add(resultMap);
            }
            return formatReturnJsonFix(result, resultList,this);
            */
            //根据参数获取列表
            String requestid = (String)dataMap.get("requestid");
            String divid =  (String)dataMap.get("divid");
            fixIslasttimesByreqid(requestid);
            List resultList = new ArrayList();
            String result = "";
            result = resultOk;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("result",resultOk);
            resultMap.put("fixMessage","数据修复成功");
            resultMap.put("divid",divid);
            resultList.add(resultMap);
            return formatReturnJsonFix(result, resultList,this);
        }else{


            /*
            String sqlStr = "";
            sqlStr +=" select hrmresource.lastname,workflow_requestbase.requestname,a.id,a.requestid,a.userid from workflow_currentoperator a   ";
            sqlStr +=" inner join hrmresource  ";
            sqlStr +=" on hrmresource.id = a.userid  ";
            sqlStr +=" inner join workflow_requestbase   ";
            sqlStr +=" on workflow_requestbase.requestid = a.requestid  ";
            sqlStr +=" where islasttimes = 0 ";
            sqlStr +=" and not exists(select 1 from workflow_currentoperator b where a.requestid = b.requestid and a.userid = b.userid and b.islasttimes = 1)  ";
            sqlStr +=" order by a.requestid,a.userid,a.isremark,a.id desc ";
            recordSet.executeSql(sqlStr);
            RecordSet rs = new RecordSet();
            String requestidtemp = "";
            String useridtemp = "";
            while(recordSet.next()){
                String id = recordSet.getString("id");
                String requestid = recordSet.getString("requestid");
                String userid = recordSet.getString("userid");
                if(!requestid.equals(requestidtemp) || !userid.equals(useridtemp)){
                    rs.executeUpdate("update workflow_currentoperator set islasttimes=1  where id = " + id );
                    requestidtemp = requestid;
                    useridtemp = userid;
                }
            }
            */

            String sqlStr = "";
            sqlStr +=" select workflow_requestbase.requestname, a.requestid  ";
            sqlStr +="   from workflow_currentoperator a  ";
            sqlStr +="  inner join workflow_requestbase  ";
            sqlStr +="     on workflow_requestbase.requestid = a.requestid  ";
            sqlStr +=" inner join hrmresource  ";
            sqlStr +=" on hrmresource.id = a.userid  ";
            sqlStr +="  where (islasttimes = 0 and not exists  ";
            sqlStr +="         (select 1  ";
            sqlStr +="            from workflow_currentoperator b  ";
            sqlStr +="           where a.requestid = b.requestid  ";
            sqlStr +="             and a.userid = b.userid  ";
            sqlStr +="             and b.islasttimes = 1)) and a.usertype=0   ";
            sqlStr +=" union   ";
            sqlStr +=" select workflow_requestbase.requestname, a.requestid  ";
            sqlStr +="   from (select requestid,userid from  workflow_currentoperator where islasttimes = 1 and usertype=0 group by requestid,userid having count(1) > 1) a  ";
            sqlStr +="  inner join workflow_requestbase  ";
            sqlStr +="     on workflow_requestbase.requestid = a.requestid  ";
            sqlStr +=" inner join hrmresource  ";
            sqlStr +=" on hrmresource.id = a.userid  ";
            recordSet.executeSql(sqlStr);
            while(recordSet.next()){
                String requestid = recordSet.getString("requestid");
                fixIslasttimesByreqid(requestid);
            }
            return checkData(dataMap);
        }
        
        
    }

    @Override
    public String getClassName() {
        return "流程请求找不到检测";
    }

    @Override
    public String getClassDesc() {
        return "检测流程请求在待办/已办/办结/我的请求中都不显示";
    }

    private void fixIslasttimesByreqid(String requestid){

        //把这个流程的所有操作者都找一遍，只要islasttimes有2个为1，就要清掉。不管客户的
        RecordSet rs001 = new RecordSet();
        RecordSet rs002 = new RecordSet();
        String sqltmp = "select userid, islasttimes, id, isremark from workflow_currentoperator where usertype=0 and requestid="+requestid+" order by userid, islasttimes,isremark desc, id asc";
        ArrayList userid2List = new ArrayList();
        ArrayList islasttimes2List = new ArrayList();
        ArrayList id2List = new ArrayList();
        ArrayList isRemark2List = new ArrayList();
        rs001.execute(sqltmp);
        while(rs001.next()){
            int userid_tmp = Util.getIntValue(rs001.getString("userid"), 0);
            int islasttimes_tmp = Util.getIntValue(rs001.getString("islasttimes"), 0);
            int id_tmp = Util.getIntValue(rs001.getString("id"), 0);
            userid2List.add(""+userid_tmp);
            islasttimes2List.add(""+islasttimes_tmp);
            id2List.add(""+id_tmp);
            isRemark2List.add(Util.null2String(rs001.getString("isremark")));
        }
        int userid_t = 0;
        int islasttimes_t = -1;
        int id_t = 0;
        int isremark_t = -1;
        for(int cx=0; cx<userid2List.size(); cx++){
            int userid_tmp = Util.getIntValue((String)userid2List.get(cx), 0);
            int islasttimes_tmp = Util.getIntValue((String)islasttimes2List.get(cx), 0);
            int id_tmp = Util.getIntValue((String)id2List.get(cx), 0);
            int isremark_tmp = Util.getIntValue((String)isRemark2List.get(cx));
            if(userid_tmp == userid_t){
                if(islasttimes_t==1 && islasttimes_tmp==1){//同用户，2个islasttimes
                    sqltmp = "update workflow_currentoperator set islasttimes=0 where id="+id_t;
                    rs002.execute(sqltmp);
                } else if (islasttimes_t == 0 && islasttimes_tmp==1 && isremark_t ==0 && (isremark_tmp == 8 || isremark_tmp == 9)) {
                    sqltmp = "update workflow_currentoperator set islasttimes=1 where id="+id_t;
                    rs002.execute(sqltmp);
                    sqltmp = "update workflow_currentoperator set islasttimes=0 where id="+id_tmp;
                    rs002.execute(sqltmp);
                }
            }else{
                if(islasttimes_t==0){//已经是不同用户了，前面用户最后一个的islasttimes还是0
                    sqltmp = "update workflow_currentoperator set islasttimes=1 where id="+id_t;
                    rs002.execute(sqltmp);
                }

            }
            userid_t = userid_tmp;
            islasttimes_t = islasttimes_tmp;
            id_t = id_tmp;
            isremark_t = isremark_tmp;
        }
        if(islasttimes_t==0){//考虑最后一个情况，如果islasttimes是0
            sqltmp = "update workflow_currentoperator set islasttimes=1 where id="+id_t;
            rs002.execute(sqltmp);
        }
    }
}



